package com.example.spirngtest.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.spirngtest.domain.DemoData;
import com.example.spirngtest.listener.DemoDataListener;
import com.example.spirngtest.service.DemoService;
import com.example.spirngtest.utils.TestFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

/**
 * @program: cet-practice
 * @description:
 * @author: 陈恩涛
 * @create: 2023-06-20 07:21
 **/

@RestController
@RequestMapping("/file")
public class FileTestController {

    @Autowired
    private DemoService demoService;

    @GetMapping("/read/simple")
    public String simpleRead() {
        String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(fileName, DemoData.class, new DemoDataListener(demoService)).excelType(ExcelTypeEnum.XLSX).sheet().doRead();

        return "success";
    }
}
